%% Temple university CIS 2033
% A modern Introduction to Probability and Statistics
% Chapter 12: The Poisson process
% Chapter 15: Data analysis: graphical summaries 
% Chapter 16: Data analysis: numerical summaries
%
% Author: Djordje Gligorijevic
% email: gligorijevic@temple.edu
%
% Special thanks to Min Xiao (http://astro.temple.edu/~tud09663/index.html)
% for given examples and ideas for lab examples.

%% Excercise 1: load poiss.mat. 
% Let the discrete random variable $X$ be the number of customers visiting
% ABC Bank in a hour. We know that $X ~ Poiss(lambda). 
% The poiss.mat stores such information: 1000 records. 
% 1.1) Estimate lambda;
% 1.2) Based on the computed lambda, randomly generate 500 data samples from
% Poiss(lambda)
% 1.3) Save the data into a mat file. 

clear;
clc;

load poiss.mat;

lambda = sum(X)/numel(X);

uds = makedist('Poisson','lambda',lambda);
ys = random(uds,[500,1]);

save poisson_generated_data ys;

%% Excercise 2: load norm.mat. 
% Let the continuous random variable $X$ be the price changes of the product XYZ. 
% Positive values mean its price is increased and negative values means its
% price is decreased. 
% We know that X ~ N(mu, sigma^2). 
% The norm.mat stores 1000 records of such information. 
% Please load the data and 
% 2.1) Estimate mu and sigma;
% 2.2) Based on the computed mu and sigma, randomly generate 500 data
% samples from N(mu, sigma^2)
% 2.3) Save the data into a mat file. 

clear;
clc;

load norm.mat;

mu = mean(X);
sigma = std(X);

uds = makedist('Normal','mu',mu,'sigma',sigma);
ys = random(uds,[500,1]);

save normal_generated_data ys;

%% Excercise 3: load oldfaithful.txt
% Replicate figure 15.2 from the Textbook, page 211.

load oldfaithful.txt

figure;
subplot(1,3,1);
histogram(oldfaithful,'BinWidth',1);
title('Bin width 2');
subplot(1,3,2);
histogram(oldfaithful,'BinWidth',30);
title('Bin width 30');
subplot(1,3,3);
histogram(oldfaithful,'BinWidth',90);
title('Bin width 90');

%% Excercise 4: load oldfaithful.txt and software.txt data
% Replicate Figure 15.10 from the textbook page 220

load oldfaithful.txt
load software.txt


figure;
subplot(1,2,1);
cdfplot(oldfaithful)
% n=length(oldfaithful);
% stairs(sort(oldfaithful),(1:n)/n)
grid on;
title('Old Faithful data');
subplot(1,2,2);
cdfplot(software)
title('Software data');
grid on;

%% Excercise 5: load drilling.txt
% Replicate Figure 15.10 from the textbook page 223

load drilling.txt

dry = drilling(:, [1 2]);
wet = drilling(:, [1 3]);

figure;
subplot(1,2,1);
scatter(dry(:,1), dry(:,2));
xlabel('Depth')
ylabel('Mean drill time')
title('Dry holes')
subplot(1,2,2);
scatter(wet(:,1), wet(:,2));
xlabel('Depth')
ylabel('Mean drill time')
title('Wet holes')

%% Excercise 6: load jankahardness.txt
% Replicate Figure 15.12 from the textbook page 224

load jankahardness.txt

figure;
scatter(jankahardness(:,1), jankahardness(:,2));
xlabel('Wood density')
ylabel('Hardness')
title('Scatterplot of Janka hardness versus density of wood')
